﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.presentation.Trees;
using System.Text;
using umbraco.BusinessLogic.Actions;
using umbraco.interfaces;
using Procure.Data.Context;

namespace Procure.Web.product
{
    public class loadProduct : BaseTree
    {
        public loadProduct(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = TreeAlias;
            rootNode.NodeID = "init";

            if (this.IsDialog)
                rootNode.Action = "javascript:openProduct(-1);";
            else
                rootNode.Action = "javascript:openDashboard('procure');";


        }

        protected override void CreateRootNodeActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionNew.Instance);
            actions.Add(ActionRefresh.Instance);
        }

        protected override void CreateAllowedActions(ref List<IAction> actions)
        {
            actions.Clear();
            //actions.Add(ActionRefresh.Instance);
            actions.Add(ActionDelete.Instance);

        }

        public override void Render(ref XmlTree tree)
        {
            try
            {
                ProcureDataContext dataStore = DataFactory.GetDataContext();
                var products = from product in dataStore.Products
                               orderby product.Name
                               select product;

                foreach (var dd in products)
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = dd.Id.ToString();
                    xNode.Text = dd.Name;

                    // Check for dialog behaviour
                    if (!this.IsDialog)
                    {
                        if (!this.ShowContextMenu)
                            xNode.Menu = null;
                        
                    }
                    else
                    {
                        if (this.ShowContextMenu)
                            xNode.Menu = new List<IAction>(new IAction[] { ActionRefresh.Instance });
                        else
                            xNode.Menu = null;
                    }
                    xNode.Action = "javascript:openProduct(" + dd.Id.ToString() + ");";
                    xNode.HasChildren = false;

                    //if (this.IsDialog)
                    //    xNode.Source = GetTreeDialogUrl(dd.Id);
                    //else
                    //    xNode.Source = GetTreeServiceUrl(dd.Id);

                    xNode.Icon = "product.gif";
                    xNode.OpenIcon = "product.gif";

                    base.OnBeforeNodeRender(ref tree, ref xNode, EventArgs.Empty);
                    if (xNode != null)
                    {
                        tree.Add(xNode);
                    }
                    base.OnAfterNodeRender(ref tree, ref xNode, EventArgs.Empty);
                }
            }
            catch (Exception) { }
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            if (!string.IsNullOrEmpty(this.FunctionToCall))
            {
                Javascript.Append("function openProduct(id) {\n");
                Javascript.Append(this.FunctionToCall + "(id)\n");
                Javascript.Append("}\n");
            }
            else if (HttpContext.Current.Request.QueryString["isDialog"] == null)
            {
                Javascript.Append(
                    @"
function openProduct(id) {
	parent.right.document.location.href = 'Procure/editProduct.aspx?id=' + id;
}
");
            }
            else
            {
                Javascript.Append(@"
function openProduct(id) {
	if (parent.opener)
		parent.opener.dialogHandler(id);
	else
		parent.dialogHandler(id);	
}
");
            }
        }

    }
}